package com.whw.cbf_qimodazuoye.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.whw.cbf_qimodazuoye.R;
import com.whw.cbf_qimodazuoye.entity.Carousel;
import com.whw.cbf_qimodazuoye.entity.CheckOrders;
import com.whw.cbf_qimodazuoye.entity.CheckOrdersAdapter;
import com.whw.cbf_qimodazuoye.entity.GlideImageLoader;
import com.whw.cbf_qimodazuoye.service.UserService;
import com.whw.cbf_qimodazuoye.utils.FaSongQingQiu;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MineActity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        Bundle bundle = this.getIntent().getExtras();
        final String userId = bundle.getString("userId");
        final String userName = bundle.getString("userName");
        final String userIntegral = bundle.getString("userIntegral");
        initUser(userId);

        Button button = findViewById(R.id.mineButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle1 = new Bundle();
                bundle1.putString("userId",userId);
                bundle1.putString("userName",userName);
                bundle1.putString("userIntegral",userIntegral);
                Intent intent=new Intent(MineActity.this,AllOrderActivity.class);
                intent.putExtras(bundle1);
                startActivity(intent);
            }
        });



        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                 R.id.navigation_home,R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(MineActity.this,MainActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                }
                    case R.id.navigation_dashboard:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(MineActity.this,RecommendationActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }


                    case R.id.navigation_notifications:
                          return true;

                }
                return false;
            }
        });

    }

    private void initUser(String userId){

        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        UserService userService = retrofit.create(UserService.class);
        Call<ResponseBody> call = userService.selectUserByUserId(userId);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        String userId =obj.getString("userId");
                        String userName =obj.getString("userName");
                        String userPhone = obj.getString("userPhone");
                        Integer userIntegral = Integer.parseInt(obj.getString("userIntegral"));
                        TextView userNameTextView = (TextView)findViewById(R.id.mineUserName);
                        TextView userPhoneTextView = (TextView)findViewById(R.id.mineUserCall);
                        TextView userIntegralTextView = (TextView)findViewById(R.id.mineUserIntegral);
                        userNameTextView.setText(userName);
                        userPhoneTextView.setText(userPhone);
                        userIntegralTextView.setText(String.valueOf(userIntegral)+"分");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }




}
